#include "DbCRUDService.h"
#include "../../tools/DbCRUD/DbUpdate.h"// 包含 DbUpdate 工具类
#include <iostream>

using grpc::Status;
using grpc::StatusCode;

// --- 更新 (Update) ---
Status DbCRUDService::UpdateUser(
    ServerContext* context,
    const UpdateUserRequest* request,
    UpdateUserResponse* response) {
    
    int32_t user_id = request->user_id();
    const ::graph::UserNode& user_updates = request->user_updates(); // 获取包含更新字段的对象

    std::string error_message;
    if (DbUpdate::UpdateUser(user_id, user_updates, error_message)) {
        response->set_success(true);
        std::cout << "Successfully updated user with ID: " << user_id << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to update user with ID: " << user_id 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::UpdateList(
    ServerContext* context,
    const UpdateListRequest* request,
    UpdateListResponse* response) {
    
    int32_t list_id = request->list_id();
    const ::graph::ListNode& list_updates = request->list_updates();

    std::string error_message;
    if (DbUpdate::UpdateList(list_id, list_updates, error_message)) {
        response->set_success(true);
        std::cout << "Successfully updated list with ID: " << list_id << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to update list with ID: " << list_id 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::UpdateTodo(
    ServerContext* context,
    const UpdateTodoRequest* request,
    UpdateTodoResponse* response) {
    
    int32_t todo_id = request->todo_id();
    const ::graph::TodoNode& todo_updates = request->todo_updates();

    std::string error_message;
    if (DbUpdate::UpdateTodo(todo_id, todo_updates, error_message)) {
        response->set_success(true);
        std::cout << "Successfully updated todo with ID: " << todo_id << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to update todo with ID: " << todo_id 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::UpdateComment(
    ServerContext* context,
    const UpdateCommentRequest* request,
    UpdateCommentResponse* response) {
    
    int32_t comment_id = request->comment_id();
    const ::graph::CommentNode& comment_updates = request->comment_updates();

    std::string error_message;
    if (DbUpdate::UpdateComment(comment_id, comment_updates, error_message)) {
        response->set_success(true);
        std::cout << "Successfully updated comment with ID: " << comment_id << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to update comment with ID: " << comment_id 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::UpdateEntity(
    ServerContext* context,
    const UpdateEntityRequest* request,
    UpdateEntityResponse* response) {
    
    // 注意：Entity 使用的是字符串 ID
    const std::string& entity_id = request->entity_id(); 
    const ::graph::EntityNode& entity_updates = request->entity_updates();

    std::string error_message;
    if (DbUpdate::UpdateEntity(entity_id, entity_updates, error_message)) {
        response->set_success(true);
        std::cout << "Successfully updated entity with ID: " << entity_id << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to update entity with ID: " << entity_id 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}